package com.jd.component.jfs.impl;

import com.jcloud.jss.JingdongStorageService;
import com.jcloud.jss.service.BucketService;
import com.jcloud.jss.service.ObjectService;
import com.jd.component.jfs.FileJfsUploadService;
import com.jd.component.jfs.vo.JfsFileVO;
import com.summer.utils.md5.Md5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.io.File;
import java.net.URI;

@Service
public class FileJfsUploadServiceImpl implements FileJfsUploadService {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    private static final String TXT_CONTENT_TYPE = "txt";
    private static final long MAX_TXT_SIZE = 1000 * 1024 * 1024;

    @Resource
    private JingdongStorageService jssModel;

    @Value("${jss.bucketName}")
    private String modelBucketName;
    @Value("${jss.endpoint}")
    private String modelEndpoint;
    @Value("${jss.urlExpireTime}")
    private Integer urlExpireTime;

    @Override
    public JfsFileVO fileUpload(JfsFileVO param, File file) {

        Assert.notNull(file, "文件未上传");
        // 校验图片格式
        String contentType = file.getName().substring(file.getName().lastIndexOf(".")+1);
        Assert.isTrue(TXT_CONTENT_TYPE.equals(contentType), "文件格式需为txt");
        // 校验大小
        long size = file.length();
        Assert.notNull(MAX_TXT_SIZE >= size, "文件不能超过1000M");

        String md5EncodedPin = Md5Util.MD5Encode(param.getErp(), "utf-8");
        long time = System.currentTimeMillis();
        String key = String.format("%s%s%d", md5EncodedPin, "_", time);

        jssModel.setEndpoint(modelEndpoint);
        boolean have = jssModel.hasBucket(modelBucketName);
        BucketService bucketService = jssModel.bucket(modelBucketName);
        if (!have) {
            bucketService.create();
        }
        bucketService.object(key).entity(file).put();
        param.setUrl(fileGetNoCheckAuth(key));
        param.setKey(key);
        return param;
    }

    @Override
    public JfsFileVO fileGet(JfsFileVO param, boolean checkAuth) {
        String key = param.getKey();
        jssModel.setEndpoint(modelEndpoint);
        ObjectService objectService = jssModel.bucket(modelBucketName).object(key);
        boolean isExist = objectService.exist();
        Assert.isTrue(isExist, "文件不存在");
        URI uri = objectService.generatePresignedUrl(urlExpireTime);
        param.setUrl(uri.toString());
        return param;
    }

    @Override
    public String fileGetNoCheckAuth(String key) {
        jssModel.setEndpoint(modelEndpoint);
        ObjectService objectService = jssModel.bucket(modelBucketName).object(key);
        boolean isExist = objectService.exist();
        if (isExist) {
            URI uri = objectService.generatePresignedUrl(urlExpireTime);
            return uri.toString();
        }
        return null;
    }
}
